﻿
using Newtonsoft.Json;
using NurseStation.common;
using NurseStation.views;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Prism.Events;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Unity;
using Prism.Mvvm;
using Prism.Commands;
using NurseStation.DataModels;
using NPOI.SS.Formula.Functions;
using NurseStation.Services;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;
using Prism.Regions;
using Coldairarrow.Util;

namespace NurseStation.viewmodels
{
    public class ColSettingViewModel : VmBase
    {
        NurseDayWorkService nurseDayWorkService = new NurseDayWorkService();
        readonly Notifiaction notifiaction = Notifiaction.Instance;
        private ColSetting Win = null;

        /// <summary>
        /// 点击保存
        /// </summary>
        public DelegateCommand<object> SaveCommand { get; private set; }

        /// <summary>
        /// 添加一行
        /// </summary>
        public DelegateCommand<object> AddCommand { get; private set; }


        private ColShow currDropItem;
        /// <summary>
        /// 当前选中下拉配置列下拉列表项
        /// </summary>
        public ColShow CurrDropItem
        {
            get { return currDropItem; }
            set { currDropItem = value; RaisePropertyChanged(nameof(CurrDropItem)); }
        }

        private ObservableCollection<ColShow> currDropList;
        /// <summary>
        /// 当前选中下拉配置列下拉列表集合 
        /// </summary>
        public ObservableCollection<ColShow> CurrDropList
        {
            get { return currDropList; }
            set
            {
                currDropList = value;
                RaisePropertyChanged(nameof(CurrDropList));
            }
        }


        /// <summary>
        /// 构造
        /// </summary>
        public ColSettingViewModel(IContainerExtension container, IEventAggregator eventAggregator, IRegionManager regionManager) : base(container, eventAggregator, regionManager)
        {
            this.SaveCommand = new DelegateCommand<object>(OnSave);
            this.AddCommand = new DelegateCommand<object>(OnAdd);
            LoadData();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            List<ColShow> listCol = nurseDayWorkService.GetDictCol();
            ObservableCollection<ColShow> dragList = new ObservableCollection<ColShow>();
            foreach (var col in listCol)
            {
                dragList.Add(col);
            }
            CurrDropList = dragList;
        }

        /// <summary>
        /// 设置窗体实例
        /// </summary>
        /// <param name="colSetting"></param>
        public void SetWin(ColSetting colSetting)
        {
            this.Win = colSetting;
        }


        /// <summary>
        /// 点击保存
        /// </summary>
        public void OnSave(object o)
        {
            List<ColShow> list = CurrDropList.ToList();
            int uprow = DatabaseContext.Db.Updateable(CurrDropList.ToList()).ExecuteCommand();
            ThemeHelper.Instance.SaveXaml(list);

            if (uprow > 0)
            {
                notifiaction.AddNotifiaction(new NotifiactionModel() { Title = "提示", Content = "保存成功！", NotifiactionType = EnumPromptType.Success });
            }
            this.Win.DialogResult = true;
            this.Win.Close();
        }

        /// <summary>
        ///添加一行配置
        /// </summary>
        /// <param name="o"></param>
        public void OnAdd(object o)
        {
            int i = CurrDropList.Count + 1;
            ColShow colShow = new ColShow() { Sort = i, Header = "Col" + i, Id = IdHelper.GetLongId() };
            long uprow = DatabaseContext.Db.Insertable(colShow).ExecuteCommand();
            if (uprow > 0)
            {
                LoadData();
                notifiaction.AddNotifiaction(new NotifiactionModel() { Title = "提示", Content = "添加成功！", NotifiactionType = EnumPromptType.Success });
            }
        }


        /// <summary>
        /// 修改护理下拉选项排序  2022-03-15
        /// </summary>
        /// <param name="preIndex">排序前index</param>
        /// <param name="index">排序后index</param>
        public void ChangeDroplistSequence(int preIndex, int index)
        {
            CurrDropList.RemoveAt(preIndex);
            CurrDropList.Insert(index, CurrDropItem);
            foreach (var a in CurrDropList)
            {
                a.Sort = CurrDropList.IndexOf(a) + 1;
            }
            DroplistSort();
        }

        /// <summary>
        /// 下拉选项排序  2022-03-15
        /// </summary>
        internal void DroplistSort()
        {
            CurrDropList = new ObservableCollection<ColShow>(CurrDropList);
            //排序
            var vw = CollectionViewSource.GetDefaultView(CurrDropList);
            if (!vw.SortDescriptions.Any(b => b.PropertyName.Equals("Sort")))
            {
                vw.SortDescriptions.Add(new SortDescription("Sort", ListSortDirection.Ascending));
            }
            vw.Refresh();
        }
    }
}
